/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author KeaneDCunha
 */
import java.io.*;
import java.net.*;
import java.util.*;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ListenerThreadHandler extends Thread
{

    private Socket socket = null;
    int port;

    public ListenerThreadHandler(Socket socket, int port) 
    {
        super("ListenerThreadHandler");
        this.socket = socket;
        this.port = port;
        System.out.println("Created new Thread to listen to peers");
    }

    @Override
    public void run() {

        boolean gotQuitPacket = false;

        try 
        {
            ObjectInputStream fromPeer = new ObjectInputStream(socket.getInputStream());
            ObjectOutputStream toPeer = new ObjectOutputStream(socket.getOutputStream());
            Packet packetFromPeer;

            while((packetFromPeer = (Packet)fromPeer.readObject()) != null)
            {
                Packet packetToPeer = null;

                if (packetFromPeer.getType() == Packet.)
                {
                    //if packet is a REQUEST

                    packetToPeer = ;
                    System.out.println("From Peer: " + packetFromPeer.getAction());

                }
                else if (packetFromPeer.getType() == Packet.)
                {

                    /* get quote from broker file */
                    packetToPeer = ;
                    System.out.println("Broker Forward: " + packetFromPeer.getAction());

                }
                //ToDo: implement more types



                System.out.println("packet type: " + packetToPeer.getType());

                /* send reply back to client */
                toPeer.writeObject(packetToPeer);

                
            }

            

            fromPeer.close();
            toPeer.close();
            socket.close();
        }
        catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }

    }
}
